/*----------------------------------------------------------------------------------------------
    标签控件练习
    1.创建label1和label2两个标签
        label1显示label2标签的长文本模式
        label2用来演示6中长文本模式
        label2注册了事件回调函数，点击label2标签，循环切换label2标签的长文本模式
    2.创建3个按钮button1、button2和button3
        按下button1加快动画速度
        按下button2在label1前面添加OK两字
        按下button3把label前面的OK删除    
-----------------------------------------------------------------------------------------------*/
#include "test.h"
#include "stdio.h"
#include "lv_symbol_def.h"

lv_obj_t *label1;       //标签1句柄
lv_obj_t *label2;       //标签2句柄

lv_obj_t *button1;      //按钮1句柄
lv_obj_t *button2;      //按钮2句柄
lv_obj_t *button3;      //按钮3句柄

const char * MODE_str[] = {"EXPAND","BREAK","DOT","SROLL","SROLL_CIRC","CROP"};

LV_FONT_DECLARE(my_font_icon);//申明字体
//定义图标
#define MY_GITHUB   "\xEE\x98\xB8"
#define MY_GRAPH    "\xEE\x98\xB9"
#define MY_RSS      "\xEE\x98\xBA"

//按钮回调函数
void Button_Event_cb(lv_obj_t *obj,lv_event_t event)
{
    if(obj == button1)//按钮 1 被按下
    {
        if(event == LV_EVENT_RELEASED)//按钮 1 被按下后松开
        {
            printf("按钮1触发，加快动画速度\r\n");
            lv_label_set_anim_speed(label2,lv_label_get_anim_speed(label2)+5);
        }
    }
    if(obj == button2)//按钮 2 被按下
    {
        if(event == LV_EVENT_RELEASED)//按钮 2 被按下后松开
        {
            printf("按钮2触发，添加OK文本\r\n");
            lv_label_ins_text(label1,0,"OK");
        }
    }
    if(obj == button3)//按钮 1 被按下
    {
        if(event == LV_EVENT_RELEASED)//按钮 3 被按下后松开
        {
            printf("按钮3触发，删除OK文本\r\n");
            lv_label_cut_text(label1,0,2);
        }
    }
}

//标签回调函数
void Label_Event_cb(lv_obj_t *obj,lv_event_t event)
{
    static lv_label_long_mode_t Mode;
    if(obj == label2)//标签 2 被按下
    {
        if(event == LV_EVENT_RELEASED)//标签 2 被按下后松开
        {
            printf("标签2触发，切换长文本模式%s\r\n",MODE_str[Mode]);
            lv_label_set_text(label1,MODE_str[Mode]);   //设置label1 的显示内容
            lv_label_set_long_mode(label2,Mode);        //更改长文本模式
            switch (Mode)
            {
                case LV_LABEL_LONG_EXPAND:
                    lv_label_set_text(label2,"EXPAND:0123456789ABCDEFGHIJKLMNOPQRSTUYWXYZ");
                    break;
                case LV_LABEL_LONG_BREAK:
                    lv_obj_set_size(label2,100,0);
                    lv_label_set_text(label2,"BREAK:0123456789ABCDEFGHIJKLMNOPQRSTUYWXYZ");
                    break;
                case LV_LABEL_LONG_DOT:
                    lv_obj_set_size(label2,100,30);
                    lv_label_set_text(label2,"DOT0123456789ABCDEFGHIJKLMNOPQRSTUYWXYZ");
                    break;   
                case LV_LABEL_LONG_SROLL:
                    lv_obj_set_size(label2,100,30);
                    lv_label_set_text(label2,"SROLL:0123456789ABCDEFGHIJKLMNOPQRSTUYWXYZ");
                    break;
                case LV_LABEL_LONG_SROLL_CIRC:
                    lv_obj_set_size(label2,100,30);
                    lv_label_set_text(label2,"SROLL_CIRC:0123456789ABCDEFGHIJKLMNOPQRSTUYWXYZ");
                    break;
                case LV_LABEL_LONG_CROP:
                    lv_obj_set_size(label2,100,30);
                    lv_label_set_text(label2,"CROP:0123456789ABCDEFGHIJKLMNOPQRSTUYWXYZ");
                    break;
                default:
                    break;
            }
            lv_obj_realign(label2);//重新屏幕对齐
            Mode++;
            if(Mode>LV_LABEL_LONG_CROP)
                Mode = LV_LABEL_LONG_EXPAND;
        }
    }
}

//测试函数
void lv_test_start(void)
{
    static lv_style_t my_style;
    printf(">>>>>>>>>>>>>>>>>>>LVGL测试程序启动test_start<<<<<<<<<<<<<<<<<<<<<<\r\n");

    lv_obj_t *SCR = lv_scr_act();//获取当前活跃的屏幕对象
    
    //创建标签--------label1-----------------
    label1 = lv_label_create(SCR,NULL);
    lv_label_set_long_mode(label1,LV_LABEL_LONG_BREAK);//长文本模式，固定宽度自动换行
    lv_obj_set_size(label1,200,0);          //设置标签大小
    lv_obj_set_pos(label1,10,50);           //设置显示位置
    lv_label_set_recolor(label1,true);      //使能文本重绘色功能
    lv_label_set_text(label1,"#ff0000 Title:#mode");        //设置文本，带有颜色重回
    lv_label_set_align(label1,LV_LABEL_ALIGN_CENTER);       //标签文本居中
    
    lv_label_set_style(label1,LV_LABEL_STYLE_MAIN,&lv_style_plain_color);//设置主背景样式
    lv_label_set_body_draw(label1,true);                    //使能背景绘制
    lv_obj_align(label1,NULL,LV_ALIGN_IN_TOP_MID,0,30);     //设置与屏幕的对齐方式

    //创建标签--------label2-----------------
    label2 = lv_label_create(SCR,NULL);
    lv_label_set_style(label2,LV_LABEL_STYLE_MAIN,&lv_style_plain_color);//设置主背景样式
    lv_label_set_body_draw(label2,true);    //使能背景绘制
    lv_obj_set_click(label2,true);          //使能点击功能
    lv_obj_set_event_cb(label2,Label_Event_cb); //设置事件回调函数
    lv_label_set_text(label2,"Please Click!");  //设置文本，带有颜色重绘
    lv_obj_align(label2,NULL,LV_ALIGN_IN_TOP_MID,0,100);     //设置与屏幕的对齐方式

    //创建按钮--------------button1-----------------
    lv_style_copy(&my_style,&lv_style_plain_color);
    my_style.text.font = &my_font_icon;
    button1 = lv_btn_create(SCR,NULL);
    lv_obj_set_pos(button1,50,150);
    lv_obj_set_size(button1,40,40);
    lv_obj_t * btn_label1 = lv_label_create(button1, NULL);
    lv_btn_set_style(btn_label1,LV_LABEL_STYLE_MAIN,&my_style);
    lv_label_set_text(btn_label1,MY_GITHUB);
    lv_obj_set_click(button1,true);     //使能点击功能
    lv_obj_set_event_cb(button1,Button_Event_cb);//设置事件回调函数
    //创建按钮--------------button2-----------------
    button2 = lv_btn_create(SCR,NULL);
    lv_obj_set_pos(button2,150,150);
    lv_obj_set_size(button2,40,40);
    lv_obj_t * btn_label2 = lv_label_create(button2, NULL);
    lv_btn_set_style(btn_label2,LV_LABEL_STYLE_MAIN,&my_style);
    lv_label_set_text(btn_label2,MY_GRAPH);
    lv_obj_set_click(button2,true);     //使能点击功能
    lv_obj_set_event_cb(button2,Button_Event_cb);//设置事件回调函数
    //创建按钮--------------button3-----------------
    button3 = lv_btn_create(SCR,NULL);
    lv_obj_set_pos(button3,250,150);
    lv_obj_set_size(button3,40,40);
    lv_obj_t * btn_label3 = lv_label_create(button3, NULL);
    lv_btn_set_style(btn_label3,LV_LABEL_STYLE_MAIN,&my_style);
    lv_label_set_text(btn_label3,MY_RSS);
    lv_obj_set_click(button3,true);     //使能点击功能
    lv_obj_set_event_cb(button3,Button_Event_cb);//设置事件回调函数
}

